package com.inspectortime.webapp.inspector.scheduleRule;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.inspectortime.calendar.Day;
import com.inspectortime.calendar.InspectorCalendar;
import com.inspectortime.domain.Inspector;
import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.repository.Repository;
import com.inspectortime.util.CalendarUtil;
import com.inspectortime.webapp.util.HttpSessionUtils;


public class ViewScheduleRulesController extends AbstractController {
	
	private static Log log = LogFactory.getLog(ViewScheduleRulesController.class);
	
	// TODO: Move to inspector configs	
	private int firstDayOfWeek = Calendar.SUNDAY;
	private Repository repository;
	private String returnView;


	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String inspectorId = request.getParameter("inspectorId"); 
		
		//Load all inspectors so we can pass them to the view
		Inspector inspector = null;
		InspectorCompany company = HttpSessionUtils.retrieveLoggedInCompany(request, repository);
		Collection<Inspector> inspectors = company.getInspectors();
		//save  a DB hit - grab the inspector from the company
		for (Inspector tempInspector : inspectors) {
			if (tempInspector.getId().equals(inspectorId)) {
				inspector = tempInspector;
				break; 
			}
		}
		
		
		//Set up dates to prepare a user's schedule
    	Calendar startCal = new GregorianCalendar();
    	boolean startParamValid = false;
    	String startDateStr = request.getParameter("start");
    	if (!StringUtils.isEmpty(startDateStr)) {
    		try {
    			startCal.setTime(CalendarUtil.formatNavDate(startDateStr));
    			startParamValid = true;
    		} catch (ParseException e) {
    			// Do nothing, use default date
    		}
    	} 
    	
    	// Default start-of-week day
    	if (!startParamValid) {
    		startCal.set(Calendar.DAY_OF_WEEK, this.firstDayOfWeek);	
    	}
    	
    	Calendar endCal = (Calendar) startCal.clone();
		endCal.add(Calendar.DATE, 6);
		
		InspectorCalendar inspectorCalendar = new InspectorCalendar(startCal.getTime(), endCal.getTime(), inspector);		
		List<Day> scheduleDays = inspectorCalendar.getDayList();
		
		//Generate calendars for navigation
		Calendar navCalPrevious = (Calendar) startCal.clone();
		navCalPrevious.add(Calendar.DATE, -7);
		Calendar navCalNext = (Calendar) startCal.clone();
		navCalNext.add(Calendar.DATE, 7);
		
		ModelAndView mv = new ModelAndView(getReturnView());
		mv.addObject("inspector", inspector);
		mv.addObject("allInspectors", inspectors);
		mv.addObject("days", scheduleDays);
		mv.addObject("navCalPrevious", navCalPrevious.getTime());
        mv.addObject("navCalNext", navCalNext.getTime());
        mv.addObject("todayCal", CalendarUtil.createDayCalendar());
		return mv;
	}
	
	public String getReturnView() {
		return returnView;
	}

	public void setReturnView(String returnView) {
		this.returnView = returnView;
	}

	public int getFirstDayOfWeek() {
		return firstDayOfWeek;
	}

	public void setFirstDayOfWeek(int firstDayOfWeek) {
		this.firstDayOfWeek = firstDayOfWeek;
	}
	
	public void setRepository(Repository repository) {
		this.repository = repository;
	}


	
}
